package org.zzh.controller.company;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.zzh.dao.common.PageBean;
import org.zzh.domain.company.SsCompany;
import org.zzh.service.company.SsCompanyService;


@Controller
@Slf4j
@RequestMapping("/company")
public class SsCompanyController {

    @Autowired
    private SsCompanyService companyService;

    @RequestMapping(value = "/list.do")
    @RequiresPermissions("企业管理")
    public ModelAndView list(
            @RequestParam(required = false,value = "page", defaultValue = "1") Integer page,
            @RequestParam(required = false,value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(required = false,value = "name", defaultValue = "") String name
    ) {
        ModelAndView modelAndView = new ModelAndView();
        PageBean<SsCompany> pageBean = companyService.list(page, pageSize, name);
        modelAndView.addObject("pageBean", pageBean);
        modelAndView.addObject("name", name);
        modelAndView.setViewName("/WEB-INF/pages/company/company-list");
        return modelAndView;
    }

    @RequestMapping(value = "/toAdd.do")
    public ModelAndView toAdd() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/WEB-INF/pages/company/company-add");
        return modelAndView;
    }


    @RequestMapping(value = "/edit.do")
    public ModelAndView edit(SsCompany company) {
        ModelAndView modelAndView = new ModelAndView();
        companyService.edit(company);
        modelAndView.setViewName("redirect:/company/list.do");
        return modelAndView;
    }

    @RequestMapping(value = "/toUpdate.do")
    public ModelAndView toUpdate(String id) {
        ModelAndView modelAndView = new ModelAndView();
        SsCompany company = companyService.findById(id);
        modelAndView.addObject("company", company);
        modelAndView.setViewName("/WEB-INF/pages/company/company-update");
        return modelAndView;
    }


    @RequestMapping(value = "/delete.do")
    public String toDelete(@RequestParam("ids") String[] ids) {
        companyService.deleteList(ids);
        return "redirect:/company/list.do";
    }

}
